package com.sgs.carbon.web.modules.common.dal.mapper.logger;

import com.sgs.carbon.common.pojo.PageResult;
import com.sgs.carbon.starter.mybatis.core.mapper.BaseMapperX;
import com.sgs.carbon.starter.mybatis.core.query.QueryWrapperX;
import com.sgs.carbon.web.modules.common.controller.logger.vo.loginlog.SysLoginLogExportReqVO;
import com.sgs.carbon.web.modules.common.controller.logger.vo.loginlog.SysLoginLogPageReqVO;
import com.sgs.carbon.web.modules.common.dal.dataobject.logger.SysLoginLogDO;
import com.sgs.carbon.web.modules.common.enums.logger.SysLoginResultEnum;

import java.util.List;

/**
 * 登陆日志 Mapper
 *
 * @author Dave Liu
 * @date 2021-10-07
 */
public interface SysLoginLogMapper extends BaseMapperX<SysLoginLogDO> {

    default PageResult<SysLoginLogDO> selectPage(SysLoginLogPageReqVO reqVO) {
        QueryWrapperX<SysLoginLogDO> query = new QueryWrapperX<SysLoginLogDO>()
                .likeIfPresent("user_ip", reqVO.getUserIp())
                .likeIfPresent("username", reqVO.getUsername())
                .betweenIfPresent("create_time", reqVO.getBeginTime(), reqVO.getEndTime());
        if (Boolean.TRUE.equals(reqVO.getStatus())) {
            query.eq("result", SysLoginResultEnum.SUCCESS.getResult());
        } else if (Boolean.FALSE.equals(reqVO.getStatus())) {
            query.gt("result", SysLoginResultEnum.SUCCESS.getResult());
        }
        query.orderByDesc("id");
        return selectPage(reqVO, query);
    }

    default List<SysLoginLogDO> selectList(SysLoginLogExportReqVO reqVO) {
        QueryWrapperX<SysLoginLogDO> query = new QueryWrapperX<SysLoginLogDO>()
                .likeIfPresent("user_ip", reqVO.getUserIp())
                .likeIfPresent("username", reqVO.getUsername())
                .betweenIfPresent("create_time", reqVO.getBeginTime(), reqVO.getEndTime());
        if (Boolean.TRUE.equals(reqVO.getStatus())) {
            query.eq("result", SysLoginResultEnum.SUCCESS.getResult());
        } else if (Boolean.FALSE.equals(reqVO.getStatus())) {
            query.gt("result", SysLoginResultEnum.SUCCESS.getResult());
        }
        query.orderByDesc("id");
        return selectList(query);
    }

}
